1623C - Balanced Stone Heaps - CodeForces Solution


binary search greedy *1600

Please click on ads to support us..

C++ Code:

#include<bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 2e5+10;

int n;
vector<int> a(N);

bool func(int num){
    vector<int> v(a.begin(), a.begin()+n);
    for (int i = n - 1; i >= 2; i--)
    {
        if(v[i] < num) return false;

        int pp = min(a[i], (v[i]-num))/3;
        v[i]-=3*pp;
        v[i-1]+=pp;
        v[i-2]+=2*pp;
    }

    // for (int i = 0; i < n; i++)
    // {
    //     cout<<v[i]<<" ";
    // }
    // cout<<endl;

    if(v[0]>=num && v[1]>=num) return true;
    else return false;
    

}

int main(){
    int t;
    cin>>t;
    while(t--){
        cin>>n;
        for (int i = 0; i < n; i++)
        {
            cin>>a[i];
        }

        int lo=0, hi=1e9, mid;
        while (hi-lo>1)
        {
            mid=(hi+lo)/2;
            if(func(mid)) lo=mid;
            else hi=mid-1;
        }
        if(func(hi)) cout<<hi<<endl;
        else cout<<lo<<endl;
        
    }
    return 0;
}


Comments

Submit
0 Comments
More Questions

129A - Cookies
1367B - Even Array
136A - Presents
1450A - Avoid Trygub
327A - Flipping Game
411A - Password Check
1520C - Not Adjacent Matrix
1538B - Friends and Candies
580A - Kefa and First Steps
1038B - Non-Coprime Partition
43A - Football
50A - Domino piling
479A - Expression
1480A - Yet Another String Game
1216C - White Sheet
1648A - Weird Sum
427A - Police Recruits
535A - Tavas and Nafas
581A - Vasya the Hipster
1537B - Bad Boy
1406B - Maximum Product
507B - Amr and Pins
379A - New Year Candles
1154A - Restoring Three Numbers
750A - New Year and Hurry
705A - Hulk
492B - Vanya and Lanterns
1374C - Move Brackets
1476A - K-divisible Sum
1333A - Little Artem